#ifndef _H_C_TCP_ACCEPTOR_H_
#define _H_C_TCP_ACCEPTOR_H_

#include "../typedefine.h"
#include "ENetErrorCode.h"
#include "../coroutine.h"
#include <memory>

namespace TB
{
	class CNetService;
	class CTcpAcceptorImpl;
	class CTcpSocket;

	class CTcpAcceptor
	{
		NON_COPYABLE(CTcpAcceptor)
		NON_MOVEABLE(CTcpAcceptor)

	public:
		CTcpAcceptor();
		~CTcpAcceptor();
		bool Init(const std::weak_ptr<CNetService>& poNetService);
		std::weak_ptr<CNetService>& GetNetService();
		const std::weak_ptr<CNetService>& GetNetService() const;
		ENetErrorCode Listen(UINT32 dwIP, UINT16 wPort, UINT32 dwBacklog); //主机字节序
		STask<std::pair<ENetErrorCode, std::shared_ptr<CTcpSocket>>> Accept();
		bool GetLocalIpPort(std::pair<UINT32, UINT16>& roIpPort) const; //主机字节序
		bool IsClosed() const;
		void Close();
		bool SetReuseAddr(bool bReuse = true);

	private:
		std::shared_ptr<CTcpAcceptorImpl> m_poImpl;
	};
}

#endif
